﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_ContatChange : CommonPageBase
    {
        List<TempFile> files = new List<TempFile>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (!string.IsNullOrEmpty(Request["p"]))
                    t1.Hidden = true;

                //txt_ATTEND.OnClientTriggerClick = Window1.GetSaveStateReference(hid_ATTEND.ClientID, txt_ATTEND.ClientID)
                //   + Window1.GetShowReference("/PublicModel/SelectUsers.aspx", "选择人员");
                txt_CUTNUB.OnClientTriggerClick = Window1.GetSaveStateReference(hid_CUTNUB.ClientID, txt_CUTNUB.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectCustom.aspx", "选择客户");

                txt_PRJNUB.OnClientTriggerClick = Window1.GetSaveStateReference(hid_PRJNUB.ClientID, txt_PRJNUB.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectProj.aspx", "选择项目");
                btnClose.OnClientClick = ActiveWindow.GetHideReference();

                DataBaseHelper.BindDrp(Drp_PERTAX, EnumData.DataBaseEunm.TaxRate);

                // DataBaseHelper.BindDrp(Drp_PAYMET, EnumData.DataBaseEunm.PayMet);

                LoadData();
                BindGrid();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["cid"]))
            {
                Proj_Main pm = TbsDbSession.Default.From<Proj_Main>().Where(Proj_Main._.ID == int.Parse(Request["cid"])).ToFirst();
                txt_PRJNUB.Text = pm.PRJNUB;
            }
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Contat Con = TbsDbSession.Default.From<Proj_Contat>().Where(Proj_Contat._.ID == int.Parse(Request["id"])).ToFirst();
                if (Con != null)
                {
                    txt_CONNUB.Text = Con.CONNUB;
                    txt_CONDES.Text = Con.CONDES;
                    txt_PRJNUB.Text = Con.PRJNUB;
                    txt_CUTNUB.Text = Con.CUTNUB;
                    txt_TAMOUT.Text = Con.TAMOUT.Value.ToString();
                    CB_CHKTAX.Checked = Con.CHKTAX.Value;
                    if (Con.IsFinsh.HasValue)
                        CB_IsFinsh.Checked = Con.IsFinsh.Value;
                    if (Con.CHKTAX.Value)
                    {
                        Drp_PERTAX.Enabled = true;
                        txt_PERTAX.Enabled = true;
                    }
                    txt_PERTAX.Text = Con.PERTAX.Value.ToString();
                    Drp_PERTAX.SelectedValue = Con.PERTAX.Value.ToString().Replace(".00","");
                    txt_AMOUNT.Text = Con.AMOUNT.Value.ToString();
                    txt_DLDATE.Text = Con.DLDATE;
                    txt_PAYMET.Text = Con.PAYMET;
                    //Drp_PAYMET.SelectedValue = Con.PAYMET;
                    txt_ADDRES.Text = Con.ADDRES;
                    txt_REMARK.Text = Con.REMARK;
                    hf.Text = Con.ATTACH;
                    txt_AddTime.Text = Con.AddTime.Value.ToString("yyyy-MM-dd");
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Proj_Contat Con = new Proj_Contat();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Con.ID = int.Parse(Request["id"]);
                Con.Attach();
                Con.CONNUB = txt_CONNUB.Text;
                Con.CONDES = txt_CONDES.Text;
                Con.PRJNUB = txt_PRJNUB.Text;
                Con.CUTNUB = txt_CUTNUB.Text;
                Con.TAMOUT = Convert.ToDecimal(txt_TAMOUT.Text);
                Con.CHKTAX = CB_CHKTAX.Checked;
                Con.PERTAX = Convert.ToDecimal(txt_PERTAX.Text);
                Con.AMOUNT = Convert.ToDecimal(txt_AMOUNT.Text);
                Con.DLDATE = txt_DLDATE.Text;
                Con.PAYMET = txt_PAYMET.Text;//Drp_PAYMET.SelectedValue;
                Con.ADDRES = txt_ADDRES.Text;
                Con.REMARK = txt_REMARK.Text;
                Con.ATTACH = hf.Text;
                Con.IsFinsh = CB_IsFinsh.Checked;
                Con.AddTime = Convert.ToDateTime(txt_AddTime.Text);
                //Con.CONNAME = txt_CONNAME.Text;
                returnvalue = TbsDbSession.Default.Update<Proj_Contat>(Con);
            }
            else
            {
                Con.DeAttach();
                Con.CONNUB = txt_CONNUB.Text;
                Con.CONDES = txt_CONDES.Text;
                Con.PRJNUB = txt_PRJNUB.Text;
                Con.CUTNUB = txt_CUTNUB.Text;
                Con.TAMOUT = Convert.ToDecimal(txt_TAMOUT.Text);
                Con.CHKTAX = CB_CHKTAX.Checked;
                Con.PERTAX = Convert.ToDecimal(txt_PERTAX.Text);
                Con.AMOUNT = Convert.ToDecimal(txt_AMOUNT.Text);
                Con.DLDATE = txt_DLDATE.Text;
                Con.PAYMET = txt_PAYMET.Text;//Drp_PAYMET.SelectedValue;
                Con.ADDRES = txt_ADDRES.Text;
                Con.REMARK = txt_REMARK.Text;
                Con.ATTACH = hf.Text;
                Con.AddID = CookInfo.EAdminid();
                Con.Adder = CookInfo.EAdminTrueName();
                Con.AddTime = DateTime.Now;
                Con.IsFinsh = CB_IsFinsh.Checked;
                //Con.CONNAME = txt_CONNAME.Text;
                Con.AddTime = Convert.ToDateTime(txt_AddTime.Text);
                returnvalue = TbsDbSession.Default.Insert<Proj_Contat>(Con);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }


        protected void CHK_TAX_CheckedChanged(object sender, EventArgs e)
        {
            float ft = 0;
            if (CB_CHKTAX.Checked)
            {
                ft = float.Parse(Drp_PERTAX.SelectedValue) / 100;
                Drp_PERTAX.Enabled = true;
            }
            else
            {
                Drp_PERTAX.SelectedIndex = 1;
                Drp_PERTAX.Enabled = false;
            }

            float z = float.Parse(txt_TAMOUT.Text);
            txt_AMOUNT.Text = ((1 + ft) * z).ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            float ft = 0;
            if (CB_CHKTAX.Checked)
                ft = float.Parse(Drp_PERTAX.SelectedValue) / 100;
            float z = float.Parse(txt_TAMOUT.Text);
            txt_AMOUNT.Text = ((1 + ft) * z).ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            float ft = 0;
            if (CB_CHKTAX.Checked)
                ft = float.Parse(Drp_PERTAX.SelectedValue) / 100;
            float z = float.Parse(txt_TAMOUT.Text);
            txt_AMOUNT.Text = ((1 + ft) * z).ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            float ft = 0;
            if (CB_CHKTAX.Checked)
                ft = float.Parse(Drp_PERTAX.SelectedValue) / 100;
            float z = float.Parse(txt_AMOUNT.Text);
            txt_TAMOUT.Text = (z / (1 + ft)).ToString();
        }

        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            string ok = "";
            if (hid_PRJNUB.Text == "1")
            {
                PageContext.RegisterStartupScript(Confirm.GetShowReference("该项目下已经存在一个合同，是否需要继续增加？", "提示", MessageBoxIcon.Question, ok, "ClearProj();"));
            }
        }

        protected void txt_CONNUB_TextChanged(object sender, EventArgs e)
        {
            Proj_Contat con = TbsDbSession.Default.From<Proj_Contat>().Where(Proj_Contat._.CONNUB == txt_CONNUB.Text).ToFirst();
            if (con != null)
            {
                Alert.Show("编号已存在，请重新输入！");
                txt_CONNUB.Text = "";
            }
        }

        protected void Drp_PERTAX_SelectedIndexChanged(object sender, EventArgs e)
        {
            float ft = 0;
            if (CB_CHKTAX.Checked)
                ft = float.Parse(Drp_PERTAX.SelectedValue) / 100;
            float z = float.Parse(txt_TAMOUT.Text);
            txt_AMOUNT.Text = ((1 + ft) * z).ToString();
        }

    }
}